<template>
  <div class="transaction-surveillance container pb">
    <p class="bread">
      <a href="#/index/lms-order-list">法律服务监控员-订单管理</a> >
      <a href="javascript:void(0);" class="font-s-18">法律服务通知单</a>
    </p>
    <div class="bg-c-fff box-border">
      <h3 class="tm-title">法律服务</h3>
      <step :stepData="stepData" :stepNum="stepNum" @stepRouter="stepRouter"></step>
    </div>
    <div class="surveillance-title-box">
      <div class="surveillance-title">法律服务监控流程图</div>
      <div class="btn-box">
        <button type="button" class="white-button" @click="message">添加监控事件</button>
      </div>
    </div>
    <div class="progress-box" v-show="companyNames.length!==0">
      <company-name-container :companyNames="companyNames"></company-name-container>
      <div class="line" :class="{center: companyNames.length===2}" :style="{height: (events.length+0.5)*90+60+'px'}">
        <div class="arrow"></div>
      </div>
      <event-item v-for="(item,index) in events" :key="index" :state="item.state" :totalSides="companyNames.length" :side="item.side" :content="item.content" :files="item.files" :isEdit="true" @edit="onEdit(item)" @delete="onDelete(item)"></event-item>
      <div class="submit-box" :class="{'tc': companyNames.length===2}">
      </div>
      <p class="tc">交易监控结束</p>
      <div class="box bg-c-fff box-border auto tc button">
        <input type="button" value="保存" class="blue-button" @click="onSubmit">
        <input type="button" value="取消" class="blue-button" @click="onCancel">
      </div>
    </div>
    <vodal :show="addEventShow" animation="zoom" :width="380" :height="310" @hide="addEventShow = false">
      <div class="vodal-xxbox">
        <h2>{{isEdit | title}}监控事件</h2>
        <div class="con">
          <div class="event-input-item">
            <label>被监控方:</label>
            <select v-model="inputSide">
              <option value="">请选择</option>
              <option value="0">甲方</option>
              <option value="1">乙方</option>
            </select>
          </div>
          <div class="event-input-item">
            <label for="numberInput">序号:</label>
            <input type="number" id="numberInput" v-model="inputNumber" step="1">
          </div>
          <div class="event-input-item">
            <label for="contentText">事件内容:</label>
            <textarea id="contentText" v-model="inputContent"></textarea>
          </div>
        </div>
      </div>
      <div class="tc auto vodal-box">
        <button class="blue-button" @click="onInputComfirm">确认</button>
        <button class="white-button" @click="addEventShow = false">取消</button>
      </div>
    </vodal>
  </div>
</template>
<script type="text/javascript">
import Step from '../components/Step'
import CompanyNameContainer from '../components/CompanyNameContainer'
import EventItem from '../components/EventItem'
export default {
  components: {
    Step,
    CompanyNameContainer,
    EventItem
  },
  data() {
    return {
      id: '',
      stepNum: 1,
      stepData: [{
        name: '法律服务通知单'
      }, {
        name: '绘制流程图'
      }, {
        name: '法律服务监控'
      }, {
        name: '服务完成'
      }],
      companyNames: [],
      events: [],
      addEventShow: false,
      isEdit: false,
      inputSide: '',
      inputNumber: '',
      inputContent: '',
      currentItem: null,
      btnShow: true,
      state: 0
    }
  },
  mounted() {
    this.id = this.$route.query.id
    this.getData()
    this.$http.get('/legalService/read/detail', {
      params: {
        id: this.$route.query.id
      }
    }).then(data => {
      if (data.data.httpCode === 200) {
        this.state = data.data.data.status
        if (data.data.data.status >= 4) {
          this.btnShow = false
        }
      } else {
        this.$toasted.show(data.data.msg)
      }
    }).catch(err => {
      console.log(err)
      this.$toasted.show('网络出错')
    })
  },
  methods: {
    onSubmit() {
      if (!this.events.length) {
        this.$toasted.show('请添加监控事件')
        return
      }
      this.$http.post('/legalService/mappingService', {
        id: this.id,
        events: JSON.stringify(this.events)
      }).then(data => {
        if (data.data.httpCode === 200) {
          this.$router.push('/lms-order-monitor?id=' + this.id)
        } else {
          this.toasted.show(data.data.msg)
        }
      }).catch(err => {
        console.log(err)
        this.$toasted.show('网络出错')
      })
    },
    onEdit(item) {
      this.currentItem = item
      this.isEdit = true
      this.inputSide = item.side
      this.inputNumber = item.index
      this.inputContent = item.content
      this.addEventShow = true
    },
    onDelete(item) {
      this.events.forEach((i, index) => {
        if (i === item) {
          this.events.splice(index, 1)
        }
      })
    },
    onInputComfirm() {
      if (this.inputSide === '') {
        this.$toasted.show('请选择被监控方')
        return
      }
      let number = parseInt(this.inputNumber)
      if (this.inputNumber === '' || isNaN(number)) {
        this.$toasted.show('请输入有效序号')
        return
      }
      let hasNumber = false
      this.events.forEach(item => {
        if (item.index === number) {
          hasNumber = true
        }
      })
      if (hasNumber && !this.isEdit) {
        this.$toasted.show('您输入的序号重复')
        return
      }
      if (this.inputContent === '') {
        this.$toasted.show('请输入事件内容')
        return
      }
      if (this.isEdit) {
        this.currentItem.side = parseInt(this.inputSide)
        this.currentItem.content = this.inputContent
        this.currentItem.index = number
      } else {
        let event = {
          side: parseInt(this.inputSide),
          files: [],
          content: this.inputContent,
          state: 1,
          index: number
        }
        this.events.push(event)
      }

      this.sortEvent()
      this.addEventShow = false
    },
    message() {
      this.addEventShow = true
      this.isEdit = false
      this.inputContent = ''
      this.inputNumber = this.events.length + 1
      this.inputSide = ''
    },
    getData() {
      this.$http.get('/legalService/read/detail', {
        params: {
          id: this.id
        }
      }).then(data => {
        if (data.data.httpCode === 200) {
          this.companyNames = [{ name: data.data.data.customerName }, { name: data.data.data.lawyerName + '律师' }]
        } else {
          this.$toasted.show(data.data.msg)
        }
      }).catch(err => {
        console.log(err)
        this.$toasted.show('网络出错')
      })
    },
    onCancel() {
      console.log('111111111111')
      window.history.back()
    },
    sortEvent() {
      this.events.sort(function(item1, item2) {
        if (item1.index > item2.index) {
          return true
        } else {
          false
        }
      })
    },
    stepRouter(i, name) {
      if (i === 0 && this.state >= 2) {
        this.$router.push('/lms-order-notice?id=' + this.$route.query.id)
      }
      if (i === 1 && this.state === 2) {
        this.$router.push('/lms-order-mapping?id=' + this.$route.query.id)
      }
      if (i === 1 && this.state > 2) {
        this.$toasted.show('请点击法律服务监控查看')
      }
      if (i === 2 && this.state >= 3) {
        this.$router.push('/lms-order-monitor?id=' + this.$route.query.id)
      }
      if (i === 3 && this.state >= 4) {
        this.$router.push('/lms-order-finish?id=' + this.$route.query.id)
      }
    }
  },
  filters: {
    title(v) {
      if (v) {
        return '编辑'
      } else {
        return '添加'
      }
    }
  }
}

</script>
<style type="text/css" scoped>
.tm-title {
  line-height: 40px;
  padding-left: 35px;
  font-size: 14px;
  color: #282828;
  border-bottom: 1px solid #ddd;
}

.pb {
  padding-bottom: 70px;
}

.surveillance-title-box {
  position: relative;
  width: 100%;
  height: 40px;
  line-height: 40px;
  margin-top: 20px;
  border-bottom: 1px solid #ddd;
  background-color: #fff;
  text-align: center;
}

.surveillance-title {
  font-size: 14px;
  color: #282828;
}

.surveillance-title-box .btn-box {
  position: absolute;
  top: 0;
  right: 100px;
  font-size: 14px;
}

.surveillance-title-box .white-button {
  width: 100px;
  height: 30px;
  line-height: 30px;
  margin-left: 10px;
}

.progress-box {
  position: relative;
  width: 100%;
  background-color: #fff;
  overflow-x: auto;
  padding-bottom: 20px;
}

.line {
  position: absolute;
  width: 2px;
  top: 0;
  left: 65px;
  margin-top: 10px;
  background-color: #cfcfcf;
}

.line.center {
  left: 50%;
}

.line .arrow {
  position: absolute;
  bottom: -10px;
  left: -7px;
  width: 0;
  height: 0;
  border-left: 8px solid transparent;
  border-right: 8px solid transparent;
  border-top: 12px solid #cfcfcf;
}

.submit-box {
  width: 100%;
  padding: 40px;
}

.submit-box button {
  width: 120px;
  height: 35px;
}

h2 {
  background-color: #d8e7ff;
  font-size: 14px;
  font-weight: bold;
  line-height: 40px;
  text-indent: 40px;
}

.con {
  margin: 10px 20px;
  padding: 13px 0 0 13px;
}

textarea {
  border: 1px solid #ddd;
  width: 240px;
  padding: 6px 10px;
  line-height: 20px;
  height: 120px;
}

.button .white-button,
.button .blue-button {
  width: 245px;
  height: 40px;
  line-height: 40px;
  text-align: center;
  margin: 40px 15px;
}

.vodal-box button {
  margin: 0 15px;
  width: 120px;
  height: 35px;
}

.event-input-item {
  min-height: 30px;
}

.event-input-item label {
  display: inline-block;
  width: 80px;
  vertical-align: top;
}

.event-input-item select {
  width: 238px;
  border: 1px solid #a5a5a5;
}

.event-input-item input {
  width: 238px;
  border: 1px solid #a5a5a5;
}

</style>
